package cn.edu.tyu.finance.thingstype.dao;

import cn.edu.tyu.finance.thingstype.domain.Users;
import cn.edu.tyu.finance.util.DbUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class UserDaoImp1 {
    public int addUser(Users users) {
        //1、获取到数据库连接
       Connection conn = DbUtil.getConnection ();
       //2、编写sql语句
        String sql ="insert into users values(?,?,?)";
        //3、获取预编译语句的执行者
        PreparedStatement ps= null;
        try {
            ps = conn.prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //4、设置参数
        try {
            ps.setString(1,users.getUserid());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            ps.setString(2, users.getUsername());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            ps.setString(3, users.getUserpsw());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //5、执行sql语句，返回影响的行数
        int res= 0;
        try {
            res = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        DbUtil.backConn(conn,ps,null);
        return res;
    }

    public static void main(String[] args){
        UserDaoImp1 userDao = new UserDaoImp1();
        Users users = new Users();
        users.setUserid("1");
        users.setUsername("zs");
        users.setUserpsw("123456");
        int res = userDao.addUser(users);
        System.out.println("res="+res);
    }
}
